package domain;
// Generated 26-jun-2009 19:31:47 by Hibernate Tools 3.2.1.GA

import java.sql.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import planner.Operations;

/**
 * Userdom generated by hbm2java
 */
@Entity
@Table(name = "userdom", catalog = "domeco")
public class Userdom implements java.io.Serializable {

    private String nick;
    private String pass;
    private String name;
    private String surname;
    private Date dateOrBirth;
    private Set<Note> notes = null;
    private Set<Bankaccount> bankaccounts = null;
    private Set<Task> tasks = null;private Set<Vehiclecar> vehiclecars = new HashSet<Vehiclecar>(0);


    public Userdom() {
    }

    public Userdom(String nick, String pass) {
        this.nick = nick;
        this.pass = pass;
    }

    public Userdom(String nick, String pass, String name, String surname, Date dateOrBirth) {
        this.nick = nick;
        this.pass = pass;
        this.name = name;
        this.surname = surname;
        this.dateOrBirth = dateOrBirth;
        //Inicializo aquí los Set`s porque al crear un nuevo usuario, las notas, cuentas, y tareas están vacías
        this.notes = new Operations().getNotesList(this);
        this.bankaccounts = new Operations().getBankaccountsList(this);
        this.tasks = new Operations().getTasksList(this);
    }

    @Id
    @Column(name = "nick", unique = true, nullable = false, length = 250)
    public String getNick() {
        return this.nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    @Column(name = "pass", nullable = false, length = 250)
    public String getPass() {
        return this.pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    @Column(name = "name", length = 250)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "surname", length = 250)
    public String getSurname() {
        return this.surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "dateOrBirth", length = 10)
    public Date getDateOrBirth() {
        return this.dateOrBirth;
    }

    public void setDateOrBirth(Date dateOrBirth) {
        this.dateOrBirth = dateOrBirth;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "userdom")
    public Set<Note> getNotes() {
        return this.notes;
    }

    public void setNotes(Set<Note> notes) {
        this.notes = notes;
    }

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="userdom")
    public Set<Vehiclecar> getVehiclecars() {
        return this.vehiclecars;
    }

    public void setVehiclecars(Set<Vehiclecar> vehiclecars) {
        this.vehiclecars = vehiclecars;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "userdom")
    public Set<Bankaccount> getBankaccounts() {
        return this.bankaccounts;
    }

    public void setBankaccounts(Set<Bankaccount> bankaccounts) {
        this.bankaccounts = bankaccounts;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "userdom")
    public Set<Task> getTasks() {
        return this.tasks;
    }

    public void setTasks(Set<Task> tasks) {
        this.tasks = tasks;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (nick != null ? nick.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Userdom)) {
            return false;
        }
        Userdom other = (Userdom) object;
        if ((this.nick == null && other.nick != null) || (this.nick != null && !this.nick.equals(other.nick))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "domain.Userdom[nick=" + nick + "]";
    }

    public Task getTask(Integer idTask) {
        Task t = null;
        for (Iterator it = this.tasks.iterator(); it.hasNext();) {
            Task a = (Task) it.next();
            if (a.getIdTask().compareTo(idTask) == 0) {
                t = a;
            }
        }
        return t;
    }

    public Bankaccount getBankaccount(int idBank) {

        Bankaccount bank = null;
        for (Iterator it = this.bankaccounts.iterator(); it.hasNext();) {
            Bankaccount b = (Bankaccount) it.next();
            if (b.getIdBank().compareTo(idBank) == 0) {
                bank = b;
            }
        }
        return bank;


    }
//Puede que este no funcione bien, hay que mirarlo

    public void removeTask(Integer idTask) {
        for (Iterator it = this.tasks.iterator(); it.hasNext();) {
            Task a = (Task) it.next();
            if (a.getIdTask().compareTo(idTask) == 0) {
                it.remove();
            }
        }

    }
//Puede que este no funcione bien, hay que mirarlo

    public Note getNote(Integer idNote) {
        Note t = null;
        for (Iterator it = this.notes.iterator(); it.hasNext();) {
            Note a = (Note) it.next();
            if (a.getIdNote().compareTo(idNote) == 0) {
                t = a;
            }
        }
        return t;
    }

//Puede que este no funcione bien, hay que mirarlo
    public void removeNote(Integer idNote) {
        for (Iterator it = this.notes.iterator(); it.hasNext();) {
            Note a = (Note) it.next();
            if (a.getIdNote().compareTo(idNote) == 0) {
                it.remove();
            }
        }

    }
}
